package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.cache.CacheManager;
import com.cskaoyan.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 创建日期: 2022/04/24 11:47
 *
 * @author ChengWenqi
 */

@Service
@Slf4j
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    @Override
    public HomePageResponse homepage() {
        HomePageResponse response = new HomePageResponse();

        try {
            // 先从 panel 表中选出所有 position 为 0 的
            Example example = new Example(Panel.class);
            example.setOrderByClause(GlobalConstants.HOME_PANEL_ORDER);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("position", GlobalConstants.HOMEPAGE_POSITION);
            List<Panel> panels = panelMapper.selectByExample(example);

            Set<PanelDto> panelDtos = new LinkedHashSet<>();
            if (!CollectionUtils.isEmpty(panels)) {
                // 再根据 panel_id 联合查找对应 content 和 item 信息
                for (Panel panel : panels) {
                    Integer panelId = panel.getId();
                    List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);
                    // 将当前 panel 转换成 dto
                    PanelDto panelDto = contentConverter.panel2Dto(panel);
                    List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                    panelDto.setPanelContentItems(panelContentItemDtos);
                    panelDtos.add(panelDto);
                }
            }
            response.setPanelContentItemDtos(panelDtos);
            // 设置成功响应码和响应信息
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("IHomeServiceImpl.homepage occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }

        return response;
    }
}
